<template>
  <div class="reportcharts">
<!--    <h2>分布-分项报表</h2>-->
    <div class="con">
      <div class="repecharts" :id="timeId" :style="'height:'+ repechartsHeight + 'px'"
      ></div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
  .reportcharts {
    width: 100%;
    height: auto;
    background: #fff;
    padding: 35px 10px 20px 35px;
    overflow: hidden;
    > h2 {
      line-height: 1;
      font-weight: 600;
      font-size: 20px;
      letter-spacing: 1px;
    }
    .echardiv{
      width: 100%;
      height: 420px;
    }
    .repecharts {
      width: 100%;
    }
    .con {
      position: relative;
      margin-top: 10px;
      .el-tabs {
        overflow: inherit;
        .el-tabs__header {
          width: 85px;
          .el-tabs__item {
            span {
              font-size: 16px;
              i {
                margin-right: 8px;
              }
            }
          }
        }
        .el-tabs__content {
          overflow: inherit;
          .tab-block{
            display: none!important;
          }
        }
      }
      .kpi{
        position: absolute;
        top: -45px;
        right:0;
        margin-right: 60px;
      }
      .kpis {
        position: absolute;
        left: 210px;
        top: -45px;
      }
    }
    .timeoption {
      position: absolute;
      right: 0;
      top: -55px;
      .on {
        .el-input--suffix {
          .el-input__inner {
            color: #e8eeff;
            background: #5580ff;
          }
        }
      }
      margin-right: 60px;
      line-height: 48px;
      label {
        font-size: 18px;
        vertical-align: top;
      }
      .el-select {
        display: inline-block;
        margin-left: 5px;
        width: 72px;
        height: 30px;
        .el-input__inner {
          box-sizing: border-box;
          height: 32px;
          text-align: center;
          padding: 0 18px 0 0;
          width: 100%;
          font-size: 14px;
        }
        .el-input {
          position: relative;
          z-index: 9;

          .el-input__suffix {
            .el-input__suffix-inner {
              line-height: 100%;
              display: inline-block;
            }
            right: 0;
            span {
              .el-select__caret {
                font-size: 12px;
                color: #8ba9ff;
              }
            }
          }
        }
      }
      .timebtn {
        float: right;
        display: block;
        height: 100%;
        padding-top: 10px;
        //margin-left: -3px;
        a,
        .el-radio {
          border: 1px solid #dbe1e5;
          border-left: none;
          margin: 0 !important;
          float: left;
          height: 30px;
          line-height: 30px;
          color: #737373;
          font-size: 14px;
          padding: 0 18px;
          cursor: pointer;
          .el-radio__input {
            display: none;
          }
          .el-radio__label {
            padding: 0;
            line-height: 30px;
          }
          &.on,
          &.is-checked {
            color: #e8eeff !important;
            .el-radio__label {
              color: #e8eeff;
            }
            background: #5580ff !important;
          }
          &:first-child {
            border-left: 1px solid #dbe1e5;
            border-radius: 5px 0 0 5px;
          }
          &:last-child {
            border-radius: 0 5px 5px 0;
          }
          &:hover {
            background: #ebebeb;
            color: #333;
            opacity: 0.9;
          }
        }
      }
    }
  }
</style>
<script>
  let echarts = require("echarts");
  let moment = require("moment");
  var Echarts = null;
  import reportGlobal from "@/components/global/reportGlobal";//公共配置
  let unit = reportGlobal.unit;
  let KPIname = reportGlobal.name;
  export default {
    props: {
      kpislist:{
        default:function () {
          return []
        }
      },
      DistrSubData:{
        default: function() {
          return []
        }
      },
      con_w:{
        default:function () {
          return ''
        }
      }
    },
    data() {
      return {
        timeId:
          Number(
            Math.random()
            .toString()
            .substr(3, 15) + Date.now()
          ).toString(36) + "Echarts",
        defaultEcharts:{            //默认样式与无数据图片
          title:{
            show:true,
            text:"暂 无 数 据",
            textAlign:'center',
            left:'48%',
            top:'45%',
            textStyle:{
              fontSize:16,
              color:'#b4b6bd',
            }
          },
          grid: {
            x: 70,
            x2: "8%"
          },
          backgroundColor:{
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#f5f8fe' // 0% 处的颜色
            }
            ],
            global: false // 缺省为 false
          },

          series: []
        },
        kpiCheckType:[],
        repechartsHeight:580,
      };
    },
    components: {},
    created() {},
    mounted() {
      //默认分时
      Echarts = echarts.init(document.getElementById(this.timeId));
      this.loadEchart(this.defaultEcharts);
    },
    beforeDestroy(){},
    watch: {
      kpislist(val){
        if(val.length){
          this.kpiCheckType = val
          this.repechartsHeight = val.length < 3 ? 580 : 900
          this.loadEchartResize(this.con_w)
        }
      },
      con_w(val){
        this.loadEchartResize(val);
      }
    },
    methods: {
      UpData(ishavedata){
        if(ishavedata==false ||this.DistrSubData.length==0){
          this.loadEchart(this.defaultEcharts);
          return
        };
        if(this.kpiCheckType.length<1) {
          this.defaultEcharts.title.text="请至少选择一个要查看的指标"
          this.loadEchart(this.defaultEcharts);
          return
        };
        if(this.kpiCheckType.includes('stay')){
          this.defaultEcharts.title.text="选择滞留人数指标无法显示饼状图"
          this.loadEchart(this.defaultEcharts);
          return
        }
        let option = {
          title : [],
          tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          toolbox:{
            right:'20',
            feature: {
              saveAsImage: {}
            }
          },
          legend: {
            width:120,
            type: 'scroll',
            orient: 'vertical',
            right: 20,
            top: 'center',
            padding:[20,0],
            data:[],
            selected:[]
          },
          series : [],
          color:['#67e6b2', '#77e7ff', '#ffc02e', '#2c69f3', '#9f70ff', '#b72cff', '#8032e2', '#e858bd', '#ea6881', '#f28e7b', '#f1b89b', '#f2dfa5', '#5db8ff', '#e7780b', '#eb5c5c', '#9dff6d', '#f3e657', '#FD8E56', '#ac2525', '#560cb3']
        };
        //动态改变了父级dom高度，等dom高度改变之后再继续执行后面的js
        this.$nextTick(()=>{
          let length = this.kpiCheckType.length;
          let center = length == 1 ? [['50%', '50%']] :
            length == 2 ? [['30%','50%'],['70%','50%']] :
              length == 3 ? [['25%',230],['75%',230],['25%',690]] : [['25%',230],['75%',230],['25%',690],['75%',690]]


          let titleCenter = length == 1 ? [['47%', '5%']] :
            length == 2 ? [['27%','10%'],['67%','10%']] :
              length == 3 ? [['22%',25],['72%',25],['22%',475]] : [['22%',25],['72%',25],['22%',475],['72%',475]]

          let radius = length == 1 ? '200' :
            length == 2 ? '150' : '150'
          for(let i in this.kpiCheckType){
            let seriesObj =  {
              name: KPIname[this.kpiCheckType[i]],
              type: 'pie',
              radius : radius,
              center: center[i],
              data:this.DistrSubData[this.kpiCheckType[i]],
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
              },
              label:{
                show:true,
              }
            };
            option.series.push(seriesObj);

            let titleObj = {
              text: KPIname[this.kpiCheckType[i]]+':',
              x:titleCenter[i][0],
              y:titleCenter[i][1]
            }
            option.title.push(titleObj);
            // console.log(this.DistrSubData[this.kpiCheckType[i]]);
            let legendData = this.DistrSubData[this.kpiCheckType[i]].map(x=>x.name)
            let selectedData = {}
            for(let i in legendData){
              selectedData[legendData[i]] = i < 6
            }
            // console.log(legendData,'legendData');
            // console.log(selectedData,'selectedData');

            option.legend.data = legendData;
            option.legend.selected = selectedData;
          }

          this.loadEchart(option);
        })
      },
      loadEchart(option) {
        Echarts.setOption(option, true);
      },
      loadEchartResize(w) {
        let opts = { width: w ,height:this.repechartsHeight};
        Echarts.resize(opts);
      },
      isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
          return false;
        } else {
          return true;
        }
      },

      //指标中是否存在%单位的
      isPer(arr,isfan=false){
        for(let i in arr) {
          if(isfan) {
            if(unit[arr[i]]!='%') {
              return true;
            }
          } else{
            if(unit[arr[i]]=='%') {
              return true;
            }
          }
        }
        return false;
      },
      //得到Max百分比
      getMaxPer(arr){
        let max=50;
        for(let i in arr) {
          if(unit[arr[i]]=='%') {
            for(let j in this.y_data[arr[i]][0]) {
              max=max > this.y_data[arr[i]][0][j] || !(this.y_data[arr[i]][0][j]) ? max : this.y_data[arr[i]][0][j];
            }
          }
        }
        let a=0;
        if(max>50) {
          a = parseInt( Math.ceil(max)% 10);
        }
        return Math.ceil(max)>50?(Math.ceil(max)+(10-a)):50;
      }
    },
  };
</script>
